package dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import org.springframework.orm.ibatis.SqlMapClientTemplate;

import util.Utilities;

import dto.ProductDTO;

public class ProductDAO {
	
	private SqlMapClientTemplate sqlMap;

	public ProductDAO(SqlMapClientTemplate sqlMap) {
		this.sqlMap = sqlMap;
	}

	public ArrayList<String> searchAllLocation() throws SQLException{
		return (ArrayList<String>) sqlMap.queryForList("product.searchAllLocation");
		
	}

	public ArrayList<String> searchAllCategory() throws SQLException {
		return (ArrayList<String>) sqlMap.queryForList("product.searchAllCategory");
	}

	public ArrayList<ProductDTO> searchByCondition(String[] byLocation, String[] byCategory) throws SQLException{
		HashMap map = new HashMap();
		map.put("location", byLocation);
		map.put("category", byCategory);
		return (ArrayList<ProductDTO>) sqlMap.queryForList("product.searchByCondition", map);
	}

	public ProductDTO searchByProductId(int productId) throws SQLException{
		return (ProductDTO) sqlMap.queryForObject("product.searchByProductId", productId);
	}

	public int getProductNoSeq() throws SQLException{
		return (Integer) sqlMap.queryForObject("product.getProductNoSeq");
	}

	public int getReadCntSeq() throws SQLException{
		return (Integer) sqlMap.queryForObject("product.getReadCntSeq");
	}

	public void registerProduct(ProductDTO pdto) throws SQLException{
		sqlMap.insert("product.registerProduct", pdto);
	}

	public ArrayList<String> getCategory() throws SQLException{
		return (ArrayList<String>) sqlMap.queryForList("product.getCategory");
	}

	public void setReadCnt(ProductDTO pdto) throws SQLException{
		sqlMap.update("product.setReadCnt", pdto);
		
	}

	public void modifyProduct(ProductDTO pdto) throws SQLException{
		sqlMap.update("product.modifyProduct", pdto);
		
	}

	public void deleteProduct(int productId) throws SQLException{
		sqlMap.delete("product.deleteProduct", productId);
		
	}

	public ArrayList<ProductDTO> searchByReadCnt() throws SQLException{
		return (ArrayList<ProductDTO>) sqlMap.queryForList("product.searchByReadCnt");
	}

	public ArrayList<ProductDTO> searchByLately(String date) throws SQLException{
		return (ArrayList<ProductDTO>) sqlMap.queryForList("product.searchByLately", date);
	}

	public ArrayList<ProductDTO> searchByRate() throws SQLException{
		return (ArrayList<ProductDTO>) sqlMap.queryForList("product.searchByRate");
	}

	public int getProductTotal() throws SQLException{
		return (Integer)sqlMap.queryForObject("product.selectTotalContent");
	}

	public ArrayList<ProductDTO> searchListByPaging(int page, String[] byLocation, String[] byCategory) throws SQLException{
		HashMap map = new HashMap();
		map.put("page", page);
		map.put("perPage",Utilities.CONTENT_PER_PAGE);
		map.put("location", byLocation);
		map.put("category", byCategory);
		
		return (ArrayList<ProductDTO>) sqlMap.queryForList("product.selectProductListByPage", map);
	}

	public void updateFileNull(int productId) throws SQLException{
		sqlMap.update("product.updateFileNull", productId);
	}
}
